##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## R code to produce graphical summaries of the data for 
##  "Every Story Has a Beginning, Middle, and an End (But Not Always in That Order): 
##   Predicting duration dynamics in a unified framework"
##
##  Authors: Daina Chiba, Nils W. Metternich, and Michael D. Ward
##
##  Last modified: 2014-08-22 
##  Contact Daina Chiba (daina.chiba@gmail.com) for any questions.
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# This R code script calculates some descriptive statistics mentioned in the text 
# and produces four descriptive graphs in .pdf format:
#
# Figure 1: Conflict recurrence over time
#   Figure1_conf_recur.pdf
# 
# Figure 5: Duration of peace and civil conflict:
#   Figure5-1_pre.pdf
#   Figure5-2_conf.pdf
#   Figure5-3_post.pdf
#

# > sessionInfo()
# R version 3.1.1 (2014-07-10)
# Platform: x86_64-apple-darwin13.1.0 (64-bit)


## Set the working directory if necessary

# setwd("your_path_to_the_folder")


# preparation -------------------------------------------------------------

rm(list=ls())
require(SPDDD)
data(ucdp.triad)


# Pre-conflict peace spells -----------------------------------------------

## Number of pre-conflict spells
length(unique(ucdp.triad $ p1.data $ spellID))

## Number of countries
length(unique(ucdp.triad $ p1.data $ ccode))

## Number of right-censored spells
table(ucdp.triad $ p1.data $ rc[ucdp.triad $ p1.data $ lo==1])

## Duration
nonc.pre.dur <- ucdp.triad $ p1.data $ total.dur[ucdp.triad $ p1.data $ rc==0 & ucdp.triad $ p1.data $ lo==1]
range(nonc.pre.dur)

## the longest non-censored post-conflict peace
unique(ucdp.triad $ p1.data $ cname[ucdp.triad $ p1.data $ total.dur == 20343])
range(ucdp.triad $ p1.data $ year[ucdp.triad $ p1.data $ total.dur == 20343])


# Conflict spells ---------------------------------------------------------

## Number of conflict spells
length(unique(ucdp.triad $ c2.data $ spellID))

## Number of countries
length(unique(ucdp.triad $ c2.data $ ccode))

## Number of right-censored spells
table(ucdp.triad $ c2.data $ rc[ucdp.triad $ c2.data $ lo==1])

## Duration of conflict
nonc.conf.dur <- ucdp.triad $ c2.data $ total.dur[ucdp.triad $ c2.data $ rc==0 & ucdp.triad $ c2.data $ lo==1]
range(nonc.conf.dur)

## the longest conflict
unique(ucdp.triad $ c2.data $ cname[ucdp.triad $ c2.data $ total.dur == 11322])
range(ucdp.triad $ c2.data $ year[ucdp.triad $ c2.data $ total.dur == 11322])


# Post-Conflict spells ----------------------------------------------------

## Number of post-conflict spells
length(unique(ucdp.triad $ p2.data $ spellID))

## Number of right-censored spells
table(ucdp.triad $ p2.data $ rc[ucdp.triad $ p2.data $ lo==1])

ucdp.triad $ p2.data $ cname[ucdp.triad $ p2.data $ rc==1 & ucdp.triad $ p2.data $ lo==1 & ucdp.triad $ p2.data $ date.d2 != "2004-12-31"]
ucdp.triad $ p2.data $ year[ucdp.triad $ p2.data $ rc==1 & ucdp.triad $ p2.data $ lo==1 & ucdp.triad $ p2.data $ date.d2 != "2004-12-31"]

## Duration of post-conflict peace
nonc.pcp.dur <- ucdp.triad$p2.data$total.dur[ucdp.triad$p2.data$rc==0 & ucdp.triad$p2.data$lo==1]
range(nonc.pcp.dur)

## the longest non-censored post-conflict peace
unique(ucdp.triad $ p2.data $ cname[ucdp.triad $ p2.data $ total.dur == 12694])
range(ucdp.triad $ p2.data $ year[ucdp.triad $ p2.data $ total.dur == 12694])


# Figure 5: Histograms of duration ----------------------------------------

pdf(file="Figure5-1_pre.pdf", width=3, height=3)
par(mar=c(2.5,2.5,.5,.5)+0.1,cex.lab=0.8,cex.axis=0.8,
    mgp=c(1.5,0.5,0), cex.main=1, cex=0.8,bg="white")
hist(nonc.pre.dur, right=F, col="gray10", br=10,
     main = "", border=F,
     xlab="Duration of Pre-Conflict Peace (Years)",axes=F)
axis(1, at= 3650*seq(from=0,to=6), label=c(0,10,20,30,40,50,60))
axis(2)
dev.off()

pdf(file="Figure5-2_conf.pdf", width=3, height=3)
par(mar=c(2.5,2.5,.5,.5)+0.1,cex.lab=0.8,cex.axis=0.8,
    mgp=c(1.5,0.5,0), cex.main=1, cex=0.8,bg="white")
hist(nonc.conf.dur, right=F, col="gray10", br=10,
     main = "", border=F,
     xlab="Duration of Civil Conflict (Years)", axes=F)
axis(1, at= 3650*seq(from=0,to=4), label=c(0,10,20,30,40))
axis(2)
dev.off()

pdf(file="Figure5-3_post.pdf", width=3, height=3)
par(mar=c(2.5,2.5,.5,.5)+0.1,cex.lab=0.8,cex.axis=0.8,
    mgp=c(1.5,0.5,0), cex.main=1, cex=0.8,bg="white")
hist(nonc.pcp.dur, right=F, col="gray10", br=10,
     main = "", border=F,
     xlab="Duration of Post-Conflict Peace (Years)",axes=F)
axis(1, at= 3650*seq(from=0,to=6), label=c(0,10,20,30,40,50,60))
axis(2)
dev.off()


# Figure 1: Bar graph of terminated conflicts -----------------------------

ntvc.post.f <- ucdp.triad $ p2.data[ucdp.triad $ p2.data $ fo==1, ]
ntvc.post.l <- ucdp.triad $ p2.data[ucdp.triad $ p2.data $ lo==1, ]

table(ntvc.post.l $ rc)
ntvc.post.l $ recurred <- ifelse(ntvc.post.l $ rc==0, 1, 0)

# date of termination for the previous conflict
ntvc.post.l $ end.date <- ntvc.post.f $ TermDay
ntvc.post.l $ term.year <- as.numeric(substring(ntvc.post.l $ end.date, 1,4))

recur.dat <- data.frame(
  year = as.numeric(colnames(table(ntvc.post.l$recurred, ntvc.post.l$term.year))),
  recur = table(ntvc.post.l$recurred, ntvc.post.l$term.year)[2,],
  not.recur = table(ntvc.post.l$recurred, ntvc.post.l$term.year)[1,])
recur.dat <- rbind(recur.dat, c(1985,0,0))
recur.dat <- recur.dat[order(recur.dat$year),]
rownames(recur.dat) <- recur.dat$year
recur.dat.t <- t(recur.dat[,c("recur","not.recur")])


pdf(file="Figure1_conf_recur.pdf", width=10, height=3.5)
par(mar=c(2.5,2.5,1,.5)+0.1,cex.lab=1,cex.axis=1,
    mgp=c(1.5,0.5,0), cex.main=1, cex=1,bg="white")
barplot(recur.dat.t, col=c("gray30","gray90"), ylim=c(0,12), ylab="Number of Terminated Conflict", cex.axis=0.9)
legend(x=11, legend=c("Recurred", "Not recurred"), cex=1, fill=c("gray30","gray90"))
dev.off()


# End of file -------------------------------------------------------------
